package com.heeexy.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.heeexy.example.service.DeviceService;
import com.heeexy.example.util.CommonUtil;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/devices")
public class DeviceController {
    @Autowired
    private DeviceService deviceService;

    /**
     * 查询设备列表
     */
    //@RequiresPermissions("devices:list")
    @GetMapping("/listDevices")
    public JSONObject listDevice(HttpServletRequest request) {
        return deviceService.listDevice(CommonUtil.request2Json(request));
    }

    /*
     * 查询设备使用情况列表
     */
    //@RequiresPermissions("devices:list")
    @GetMapping("/listUseTime")
    public JSONObject listUseTime(HttpServletRequest request) {
        return deviceService.listUseTime(CommonUtil.request2Json(request));
    }

    /**
     * 新增设备
     */
    //@RequiresPermissions("devices:add")
    @PostMapping("/addDevices")
    public JSONObject addDevice(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "devId, devModel, devName, devBrand, devType, devPosition, devSituation, devAdminId");
        return deviceService.addDevice(requestJson);
    }

    /**
     * 修改设备
     */
    //@RequiresPermissions(value = {"devices:add","devices:update"},logical = Logical.OR)
    @PostMapping("/updateDevices")
    public JSONObject updateDevice(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "devName,devPosition,devSituation,devId,devModel");
        return deviceService.updateDevice(requestJson);
    }

    /**
     * 删除设备
     */
    //@RequiresPermissions("devices:delete")
    @PostMapping("/deleteDevices")
    public JSONObject deleteDevice(@RequestBody JSONObject requestJson){
        CommonUtil.hasAllRequired(requestJson, "devId,deleteStatus");
        return deviceService.deleteDevice(requestJson);
    }

    @PostMapping("/applyDevices")
    public JSONObject applyDevice(@RequestBody JSONObject requestJson){
        CommonUtil.hasAllRequired(requestJson,
                "devId,user_id,devFunction,start,end,isAnxious");
        return deviceService.applyDevice(requestJson);
    }

    @GetMapping("/listApply")
    public JSONObject listApply(HttpServletRequest request) {
        return deviceService.listApply(CommonUtil.request2Json(request));
    }


    @PostMapping("/updateApply")
    public JSONObject updateApply(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "applyId,situation");
        return deviceService.updateApply(requestJson);
    }

    @GetMapping("/listRepair")
    public JSONObject listRepair(HttpServletRequest request) {
        return deviceService.listRepair(CommonUtil.request2Json(request));
    }


    @PostMapping("/updateRepair")
    public JSONObject updateRepair(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "Id, situation, deleteStatus");
        return deviceService.updateRepair(requestJson);
    }

    @PostMapping("/searchDevice")
    public JSONObject searchDevice(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "key");
        return deviceService.searchDevice(requestJson);
    }

    @PostMapping("/listApplyUser")
    public JSONObject listApplyUser(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "state,userId");
        return deviceService.listApplyUser(requestJson);
    }

    @PostMapping("/deleteApply")
    public JSONObject deleteApply(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "appId");
        return deviceService.deleteApply(requestJson);
    }

    @GetMapping("/getDevicePosition")
    public JSONObject getDevicePosition(HttpServletRequest request) {
        return deviceService.getDevicePosition(CommonUtil.request2Json(request));
    }

    @PostMapping("/getDeviceName")
    public JSONObject getDeviceName(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "devPosition");
        return deviceService.getDeviceName(requestJson);
    }

    @PostMapping("/getDeviceId")
    public JSONObject getDeviceId(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "devPosition,devName");
        return deviceService.getDeviceId(requestJson);
    }

    @PostMapping("/getDeviceDetail")
    public JSONObject getDeviceDetail(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "devId");
        return deviceService.getDeviceDetail(requestJson);
    }

    @PostMapping("/getDeviceFunction")
    public JSONObject getDeviceFunction(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "devId");
        return deviceService.getDeviceFunction(requestJson);
    }
}
